package com.example.wan1.base.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.wan1.interfaces.RvScrollListener;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/***
 * @Author: zxd
 * @CreateDate: 2020/6/16 11:14
 * @Description:
 */
public abstract class BaseFragment extends Fragment {

    protected Unbinder mUnbinder;
    protected View mRootView;

    protected abstract @LayoutRes
    int initLayoutRes();

    protected abstract void initData();

    protected abstract void initView();

    protected Context mContext;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(initLayoutRes(), container, false);
        mUnbinder = ButterKnife.bind(this, mRootView);
        initView();
        return mRootView;
    }

    //懒加载部分
    private boolean isLoaded = false;
    private boolean isFirst = true;

    @Override
    public void onResume() {
        super.onResume();
        if (!isLoaded && !isHidden() && isFirst) {
            initData();
            isLoaded = true;
            isFirst = false;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mUnbinder != null)
            mUnbinder.unbind();
        isLoaded = false;
    }

    //需要添加隐藏的方法
    protected RecyclerView.OnScrollListener mOnScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            RecyclerView.LayoutManager layout = recyclerView.getLayoutManager();
            if (layout instanceof StaggeredGridLayoutManager) {
                int[] position = ((StaggeredGridLayoutManager) layout).findFirstCompletelyVisibleItemPositions(null);
                if (position[0] == 0) {
                    //如果position为0表示列表正处于顶部
                    mRvScrollListener.inTop(true, recyclerView);
                } else {
                    mRvScrollListener.inTop(false, recyclerView);
                }
            } else if (layout instanceof LinearLayoutManager) {
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                //得到当前列表第一个完全显示的item的position
                int position = layoutManager.findFirstCompletelyVisibleItemPosition();
                if (position == 0) {
                    //如果position为0表示列表正处于顶部
                    mRvScrollListener.inTop(true, recyclerView);
                } else {
                    mRvScrollListener.inTop(false, recyclerView);
                }
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            //判断滑动方向，recycleView item 上拉 下滑不同动画
            RecyclerView.Adapter mAdapter = recyclerView.getAdapter();
            if (dy > 0) {
                isUp = true;
            } else {
                isUp = false;
            }
            //过滤掉一些缓慢的滑动
            if (Math.abs(dy) > 10) {
                //滑动方向
                mRvScrollListener.scrollType(dy > 0);
            }
        }
    };

    protected RvScrollListener mRvScrollListener;
    private boolean isUp;
}
